Java For Thinkers: Master The Art Of Programming by Mr Kotiyana

Java For Thinkers: Master The Art Of Programming by Mr Kotiyana

Author:Mr Kotiyana [Kotiyana, Mr]
Language: eng
Format: epub
Published: 2017-04-10T07:00:00+00:00


binary_search(A, target):

lo = 1, hi = size(A)

while lo <= hi:

mid = lo + (hi-lo)/2

if A[mid] == target:

return mid

else if A[mid] < target:

lo = mid+1

else:

hi = mid-1

// target was not found

Complexity:

Since each comparison binary search uses halves the search space, we can assert and easily prove that binary search will never use more than (in big-oh notation) O(log N) comparisons to find the target value.

The logarithm is an awfully slowly growing function. In case you’re not aware of just how efficient binary search is, consider looking up a name in a phone book containing a million names. Binary search lets you systematically find any given name using at most 21 comparisons. If you could manage a list containing all the people in the world sorted by name, you could find any person in less than 35 steps.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.